<template>
    <div>
        <h2>添加病房信息</h2>
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td>病房号(病房名称)</td>
                    <td><input type="text" v-model="pdata.roomNumber" /></td>
                </tr>
                <tr>
                    <td>科室</td>
                    <td>
                        <select v-model="pdata.tid">
                            <option value=-1>请选择</option>
                            <option v-for="item in pbind" :key="item.tid" :value="item.tid">{{ item.tname }}</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>床位总数两</td>
                    <td><input type="text" v-model="pdata.begNum" /></td>
                </tr>
                <tr>
                    <td>状态</td>
                    <td>
                        <input type="radio" v-model="pdata.state" value="1" />启用
                        <input type="radio" v-model="pdata.state" value="0" />维修
                    </td>
                </tr>
                <tr>
                    <td>空闲床数量</td>
                    <td><input type="text" v-model="pdata.kongNum" /></td>
                </tr>
                <tr>
                    <td>创建日期</td>
                    <td><input type="date" v-model="pdata.strDate" /></td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="button" value="添加" @click="AddHouse()" /></td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import axios from 'axios';
import { ref, reactive, onMounted } from 'vue';
import { useRouter } from 'vue-router';

const router = useRouter();

let pdata = reactive({
    roomNumber: "",
    tid: - 1,
    begNum: null,
    state: null,
    statess: 1,//是否满人  默认为未满
    kongNum: null,
    strDate: null
})

let pbind = ref([]);

onMounted(() => {
    Binds();
})

const AddHouse = () => {

    if (pdata.roomNumber == null) {
        alert("病房号不能为空");
        return;
    }

    if (pdata.tid == -1) {
        alert("科室不能为空");
        return;
    }

    if (pdata.begNum == null) {
        alert("床位总数不能为空");
        return;
    }

    if (pdata.state == null) {
        alert("状态不能为空");
        return;
    }

    if (pdata.kongNum == null) {
        alert("空闲床数量不能为空");
        return;
    }

    if (pdata.strDate == null) {
        alert("创建日期不能为空");
    }

    if (pdata.kongNum == 0) {
        pdata.statess = 0;
    }
    if (pdata.kongNum > 0) {
        pdata.statess = 1;
    }

    if(pdata.roomNumber.length>6){
        alert("病房号不能超过6位");
        return;
    }


    axios({
        url: "/api/Hotel/AddHouse",
        method: "post",
        data: pdata
    }).then((res) => {
        if (res.data > 0) {
            alert("添加成功");
            router.push("/ShowHouseView");
        } else {
            alert("添加失败");
        }
    })
}


const Binds = () => {
    axios({
        url: "/api/Hotel/Binds",
        method: "get"
    }).then((res) => {
        pbind.value = res.data;

    })
}





</script>

<style scoped></style>